<?php
/*
导出模块
*/
namespace Home\Controller;
use Think\Controller;
class ExcelController extends Controller{
  public function exportExcel($expTitle,$expCellName,$expTableData){  //excel导出方法
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  打印时间:'.date('Y-m-d H:i:s'));

        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

    }




    public function search(){
      $user_auth = session('USER_AUTH');

      $parent = I('get.parentbank');
      $this->assign('parent',$parent);

      $child = I('get.childbank');
      $this->assign('child',$child);

      $send_start = I('get.sendstart');
      $this->assign('send_start',$send_start);

      $send_end = I('get.sendend');
      $this->assign('send_end',$send_end);

      $money_start = I('get.moneystart');
      $this->assign('money_start',$money_start);

      $money_end = I('get.moneyend');
      $this->assign('money_end',$money_end);

      $market = I('get.market');
      $this->assign('market',$market);

      $sign = I('get.sign');
      $this->assign('sign',$sign);

      $place = I('get.place');
      $this->assign('place',$place);

      $supervise = I('get.supervise');
      $this->assign('supervise',$supervise);

      $send_start = timeStrop($send_start);
      $send_end = timeStrop($send_end);
      $money_start = timeStrop($money_start);
      $money_end = timeStrop($money_end);

        if($user_auth['roleid']==3){
    	    if(!empty($user_auth['banks'])){
    	        $where['bank_id'] = array('IN',$user_auth['banks']);
    	    }
      	}
      	elseif($user_auth['roleid']==7 || $user_auth['roleid']==11){
      		$where['place']=array('eq',$user_auth['place']);
      	}
        elseif($user_auth['roleid']==13){//江宁负责人
            $where['place|turncity']= '2';
        }
        elseif($user_auth['roleid']==14){//江北负责人
            $where['_string'] = "place=3 OR turncity=1";
        }
      if($parent){
            $where['bank_id'] = $parent;
        }
      if($child){
          $where['part_bank_id'] = $child;
      }
      if($send_start && $send_end){
          $where['audit_time'] = array(array('elt',$send_end) , array('egt',$send_start));
      }
      elseif($send_start && empty($send_end)){
          $where['audit_time'] = array('egt',$send_start);
      }
      elseif(empty($send_start) && $send_end){
          $where['audit_time'] = array('elt',$send_end);
      }
      if($money_start && $money_end){
          $where['credit_real_time'] = array(array('elt',$money_end) , array('egt',money_start));
      }
      elseif($money_start && empty($money_end)){
          $where['credit_real_time'] = array('egt',$money_start);
      }
      elseif(empty($money_start) && $money_end){
          $where['credit_real_time'] = array('elt',$money_end);
      }
      if($market){
          $user = D('User') -> findSameUser($market);
          $where['market_id'] = $user['user_id'];
      }
      if($sign){
          $user = D('User') -> findSameUser($sign);
          $where['sign_id'] = $user['user_id'];
      }
      if($supervise){
        $where['supervise']=$supervise;
      }
      if($user_auth['roleid']==4  && $place){
        if($place==1){
          $where['_string'] = "place=1 and turncity=0";
        }
        if($place==2){
          //$data['place&turncity']=array('2','0','_multi'=>true);
          $where['_string'] = "place='1' and turncity='2' or place='2' ";
        }
        if($place==3){
          $where['_string'] = 'place=3 or place=1 and turncity=1';
        }
      }
      $where['isdel']='0';
      $docs =M('document')->where($where)->order('doc_id desc')->select();
      // p($where);
      // echo M('document')->_sql();
      // die;
      $xlsName  = "查询统计";//表格文件的名字
      $xlsCell  = array(
                        array('xuhao','序号'),
                        array('realname','姓名'),
                        array('supervise','监管类型'),
                        array('zong','贷款银行'),
                        array('zhi','贷款支行'),
                        array('busyamount','商业贷款金额'),
                        array('summoney','贷款总金额'),
                        array('market','营销人'),
                        array('sign','签单人'),
                        array('sendtime','送审时间'),
                        array('realtime','实际放款时间')
                      );//单元格的内容
    foreach($docs as $k => $v){
      $xlsData[]=array(
          'xuhao'  => $k+1,
          'realname' => $v['buy_name'],
          'supervise' => $v['supervise']=='1'?'贷款监管':$v['supervise']=='2'?'全款监管':'不监管' ,
          'zong'  => getBankName($v['bank_id']),
          'zhi'   => getBankName($v['part_bank_id']),
          'busyamount'  => getMoney($v['doc_id']),
          'summoney'    => $v['amount'],
          'market'      => getUsername($v['market_id']),
          'sign'       => getUsername($v['sign_id']),
          'sendtime'    => $v['audit_time']?turnTime($v['audit_time']):'',
          'realtime'   => $v['credit_real_time']?turnTime($v['credit_real_time']):''

          );
    }

    $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }








}
